package com.baihejia.common.dubbo.api;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.baihejia.common.dubbo.domain.City;
import com.baihejia.common.dubbo.domain.CommunityEsPage;
import com.baihejia.common.dubbo.domain.CommunityQuery;
import com.baihejia.common.dubbo.domain.Page;
import com.baihejia.common.dubbo.libs.JsonHelper;
import com.baihejia.common.dubbo.service.Api;

import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 消费者
 *
 * @author hanxipeng
 */
public class CommonClient {

    public static Api api;

    public static void init(String app, Integer dubboQosPort, String zkHosts) {
        System.setProperty("dubbo.qos.port", String.valueOf(dubboQosPort));
        ReferenceConfig<Api> reference = new ReferenceConfig<>();
        reference.setApplication(new ApplicationConfig(String.format("%s-%s", app, dubboQosPort)));
        reference.setRegistries(Arrays.asList(zkHosts.split(";|,")).stream().map(zkHost -> new RegistryConfig(String.format("zookeeper://%s", zkHost))).collect(Collectors.toList()));
        reference.setInterface(Api.class);
        reference.setVersion("1.0.0");
        reference.setRetries(0);
        reference.setTimeout(30000);
        reference.setOwner(app);
        api = reference.get();
    }

    public static void main(String[] args) {
//        Client.init("hehe", 33333, "118.89.226.86:2181");
////        System.out.println(api.openCities());
////        api.rebuildHouseIndex();
//        CommunityQuery communityQuery = new CommunityQuery();
//        communityQuery.cityId = 1;
//        communityQuery.limit = 100;
//        CommunityEsPage page = api.getCommunitys(communityQuery);
//        System.out.println(JsonHelper.toString(page));
//        City city = api.city(1);
//        System.out.println(JsonHelper.toString(city));
    }
}
